package com.yd.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.pojo.Citys;
import com.yd.service.CitysService;
import com.yd.tool.UtsHanziToPinyin;
import com.yd.tool.UtsReg;

@Controller
public class CitysControllerSys {
	@Resource
	private CitysService citysService;

	
	@RequestMapping(value = {"/sys/citys/list"})
	@ResponseBody
	public String sys_citys_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String so="";
		String page="";
		String size="";
		String pid="0";
		if(UtsReg.getStringKB(request.getParameter("so"))){
			so=request.getParameter("so").trim();
		}
		if(UtsReg.getIntXl(request.getParameter("page"))){
			page=request.getParameter("page").trim();
		}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){
			size=request.getParameter("size").trim();
		}else{size = "10";}
		if(UtsReg.getInt(request.getParameter("pid"))){
			pid=request.getParameter("pid").trim();
		}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));		
		
		List<Citys> list = null;
		EntityWrapper<Citys> ew=new EntityWrapper<Citys>();
		if(so.length()>0){
			ew.like("title", so);
		}else{
			ew.eq("pid", pid);
		}
		
		list=citysService.selectList(ew);
		PageInfo<Citys> pages = new PageInfo<Citys>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping(value = {"/sys/citys/add"})
	@ResponseBody
	public String sys_citys_add(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String idstr = "";
		String title = "";
		String pid="";
		
		if(UtsReg.getInt(request.getParameter("pid"))){
			pid=request.getParameter("pid").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "p参数错误");return jsonObject.toString();
		}

		if(UtsReg.getInt(request.getParameter("idstr"))){
			idstr=request.getParameter("idstr").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringTitle(request.getParameter("title"))){
			title=request.getParameter("title").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "标题格式错误");return jsonObject.toString();
		}
		
		String tid="0";
		Citys c=null;
		if(!pid.equals("0")){
			c=citysService.selectOne(new EntityWrapper<Citys>().eq("idstr", pid));
			tid=String.valueOf(Integer.valueOf(c.getTid())+1);
		}
		
		Citys citys=new Citys();
		citys.setTid(tid);
		citys.setIdstr(idstr);
		citys.setPid(pid);
		citys.setTitle(title);
		citys.setQuanpin(UtsHanziToPinyin.toHanyuPinyin(title));
		citys.setShouzimu(UtsHanziToPinyin.getFirstLetter(title));
		
		if(!citysService.insert(citys)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping("/sys/citys/upbyid")
	@ResponseBody
	public String sys_citys_upbyid(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject(); 
		
		String id="";
		String idstr="";
		String title="";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "编号格式错误");return jsonObject.toString();
		}

		if(UtsReg.getInt(request.getParameter("idstr"))){
			idstr=request.getParameter("idstr").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringTitle(request.getParameter("title"))){
			title=request.getParameter("title").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "标题格式错误");return jsonObject.toString();
		}
		
		
		Citys citys=new Citys();
		
		citys.setId(Integer.valueOf(id));
		citys.setIdstr(idstr);
		citys.setTitle(title);
		citys.setQuanpin(UtsHanziToPinyin.toHanyuPinyin(title));
		citys.setShouzimu(UtsHanziToPinyin.getFirstLetter(title));
		
		if(!citysService.updateById(citys)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping("/sys/citys/delbyid")
	@ResponseBody
	public String sys_citys_delbyid(HttpServletRequest request) {
		
		JSONObject jsonObject = new JSONObject(); 
		
		String id="";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误");return jsonObject.toString();
		}
		
		Citys citys=citysService.selectById(id);
		
		if(citys==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "无此数据");return jsonObject.toString();
		}
		
		
		
		if(citysService.selectCount(new EntityWrapper<Citys>().eq("pid", citys.getIdstr()))>0){
			jsonObject.put("error", "1");jsonObject.put("msg", "下级有数据不可以删除");return jsonObject.toString();
		}
		
		if(!citysService.deleteById(id)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping("/sys/citys/upHot")
	@ResponseBody
	public String sys_citys_upHot(HttpServletRequest request) {
		
		JSONObject jsonObject = new JSONObject(); 
		
		String id="";
		String num="0";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误");return jsonObject.toString();
		}
		
		if(UtsReg.getInt(request.getParameter("num"))){
			num=request.getParameter("num").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误2");return jsonObject.toString();
		}
		
		if(Integer.valueOf(num)>0){
			num="1";
		}
		
		Citys citys=new Citys();
		citys.setId(Integer.valueOf(id));
		citys.setHot(num);
		
		if(!citysService.updateById(citys)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
}
